﻿using System;
using System.Collections.Generic;
using System.Collections;
using Utilities.Log;
using System.Configuration;
namespace Wx.Core
{
    public static class Config
    {
        private static LogHelper log = new LogHelper(typeof(Config));

        public static string appid = string.Empty;
        public static string secret = string.Empty;
        public static string token = string.Empty;
        public static string access_token = string.Empty;
        public static DateTime get_access_time = DateTime.Now;
        public static List<string> safe_ip_list;
        public static Hashtable apiurl = new Hashtable();

        public static void Init(string file)
        {
            try
            {
                appid = System.Configuration.ConfigurationManager.AppSettings["appid"];
                secret = System.Configuration.ConfigurationManager.AppSettings["secret"];
                token = System.Configuration.ConfigurationManager.AppSettings["token"];
            }
            catch (Exception e)
            {
                log.error(e);
            }
            apiurl.Add("gettoken", "https://api.weixin.qq.com/cgi-bin/token?grant_type={0}&appid={1}&secret={2}");
            apiurl.Add("createmenu", "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}");
            apiurl.Add("web_getcode", "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state={3}#wechat_redirect");
            apiurl.Add("web_gettoken", "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code");
            apiurl.Add("refresh_token", "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}");
            apiurl.Add("getusermsg", "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang={2}");
            apiurl.Add("qrticket", "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}");
            apiurl.Add("getqr", "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}");
        }

        public static string GetApiUrl(string key)
        {
            try
            {
                return (string)apiurl[key.ToLower()];
            }
            catch (Exception e)
            {
                log.error("Get Api Url Error:" + e);
                return string.Empty;
            }
        }
    }
}
